<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>如何使用布尔匹配 | Elasticsearch: 权威指南 | Elastic</title>
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
</head>
<body>
<div class="main-container">
    <section id="content">
        
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原文地址: <a href="https://www.elastic.co/guide/cn/elasticsearch/guide/current/_how_match_uses_bool.html" rel="nofollow">https://www.elastic.co/guide/cn/elasticsearch/guide/current/_how_match_uses_bool.html</a>, 版权归 www.elastic.co 所有<br/>
                            英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/guide/current/_how_match_uses_bool.html" rel="nofollow">https://www.elastic.co/guide/en/elasticsearch/guide/current/_how_match_uses_bool.html</a>
                            </div>
                        <!-- start body -->
                  <div class="page_header">
<b>请注意:</b><br>本书基于 Elasticsearch 2.x 版本，有些内容可能已经过时。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch: 权威指南</a></span>
»
<span class="breadcrumb-link"><a href="search-in-depth.html">深入搜索</a></span>
»
<span class="breadcrumb-link"><a href="full-text-search.html">全文搜索</a></span>
»
<span class="breadcrumb-node">如何使用布尔匹配</span>
</div>
<div class="navheader">
<span class="prev">
<a href="bool-query.html">« 组合查询</a>
</span>
<span class="next">
<a href="_boosting_query_clauses.html">查询语句提升权重 »</a>
</span>
</div>
<div class="section">
<div class="titlepage"><div><div>
<h2 class="title">
<a id="_how_match_uses_bool"></a>如何使用布尔匹配<a class="edit_me edit_me_private" rel="nofollow" title="Editing on GitHub is available to Elastic" href="https://github.com/elasticsearch-cn/elasticsearch-definitive-guide/edit/cn/100_Full_Text_Search/20_How_match_uses_bool.asciidoc">edit</a>
</h2>
</div></div></div>
<p>目前为止，可能已经意识到<a class="xref" href="match-multi-word.html" title="多词查询">多词 <code class="literal">match</code> 查询</a>只是简单地将生成的 <code class="literal">term</code> 查询包裹在一个 <code class="literal">bool</code> 查询中。如果使用默认的 <code class="literal">or</code> 操作符，每个 <code class="literal">term</code> 查询都被当作 <code class="literal">should</code> 语句，这样就要求必须至少匹配一条语句。以下两个查询是等价的：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "match": { "title": "brown fox"}
}</pre>
</div>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}</pre>
</div>
<p>如果使用 <code class="literal">and</code> 操作符，所有的 <code class="literal">term</code> 查询都被当作 <code class="literal">must</code> 语句，所以 <em>所有（all）</em> 语句都必须匹配。以下两个查询是等价的：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "match": {
        "title": {
            "query":    "brown fox",
            "operator": "and"
        }
    }
}</pre>
</div>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "bool": {
    "must": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }}
    ]
  }
}</pre>
</div>
<p>如果指定参数 <code class="literal">minimum_should_match</code> ，它可以通过 <code class="literal">bool</code> 查询直接传递，使以下两个查询等价：</p>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
    "match": {
        "title": {
            "query":                "quick brown fox",
            "minimum_should_match": "75%"
        }
    }
}</pre>
</div>
<div class="pre_wrapper lang-js">
<pre class="programlisting prettyprint lang-js">{
  "bool": {
    "should": [
      { "term": { "title": "brown" }},
      { "term": { "title": "fox"   }},
      { "term": { "title": "quick" }}
    ],
    "minimum_should_match": 2 <a id="CO62-1"></a><i class="conum" data-value="1"></i>
  }
}</pre>
</div>
<div class="calloutlist">
<table border="0" summary="Callout list">
<tr>
<td align="left" valign="top" width="5%">
<p><a href="#CO62-1"><i class="conum" data-value="1"></i></a></p>
</td>
<td align="left" valign="top">
<p>因为只有三条语句，<code class="literal">match</code> 查询的参数 <code class="literal">minimum_should_match</code> 值 75% 会被截断成 <code class="literal">2</code> 。即三条 <code class="literal">should</code> 语句中至少有两条必须匹配。</p>
</td>
</tr>
</table>
</div>
<p>当然，我们通常将这些查询用 <code class="literal">match</code> 查询来表示，但是如果了解 <code class="literal">match</code> 内部的工作原理，我们就能根据自己的需要来控制查询过程。有些时候单个 <code class="literal">match</code> 查询无法满足需求，比如为某些查询条件分配更高的权重。我们会在下一小节中看到这个例子。</p>
</div>
<div class="navfooter">
<span class="prev">
<a href="bool-query.html">« 组合查询</a>
</span>
<span class="next">
<a href="_boosting_query_clauses.html">查询语句提升权重 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>